﻿using ElsProp.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ElsProp
{
    /// <summary>
    /// zhuangbeilan.xaml 的交互逻辑
    /// </summary>
    public partial class zhuangbeilan : UserControl
    {
        /// <summary>
        /// 装备栏类型
        /// </summary>
        public EnumZhuangBeiLan zhuangBeiLanType {  get; set; }
        public bool mouseFlag {  get; set; }
        public zhuangbeilanmode mode { get; set; }
        public event Action<zhuangbeilanmode> change;
        public event Action<zhuangbeilanmode, EnumZhuangBeiLan> click;
        public zhuangbeilan()
        {
            InitializeComponent();
            this.AllowDrop = true;
            //this.MouseDown += Zhuangbeilan_MouseDown;
            //this.MouseMove += Zhuangbeilan_MouseMove;
            //this.MouseUp += Zhuangbeilan_MouseUp;
            can.MouseDown += Zhuangbeilan_MouseDown;
           // can.MouseMove += Zhuangbeilan_MouseMove;
           // can.MouseUp += Zhuangbeilan_MouseUp;
            can.AllowDrop = true;
            can.Drop += Can_Drop;
            mode = this.DataContext as zhuangbeilanmode;
            this.Loaded += Zhuangbeilan_Loaded;
          
        }

        private void Zhuangbeilan_Loaded(object sender, RoutedEventArgs e)
        {
            mode.zhuangBeiLanType = zhuangBeiLanType;
        }

        public void ValueChange()
        {
            change?.Invoke(mode);
        }
        private void Can_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(PropModel)))
            {
                var data = e.Data.GetData(typeof(PropModel)) as PropModel;
                if (data.BuWei == zhuangBeiLanType)
                {
                    mode.propModels = data;
                    change?.Invoke(mode);
                }
            }
        }

        private void Zhuangbeilan_MouseUp(object sender, MouseButtonEventArgs e)
        {
            mouseFlag = false;
        }

        private void Zhuangbeilan_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouseFlag)
            {
                DragDrop.DoDragDrop(this,
                             100,
                             DragDropEffects.Copy);
            }
        }

        private void Zhuangbeilan_MouseDown(object sender, MouseButtonEventArgs e)
        {
            mouseFlag = true;
            if (e.RightButton==MouseButtonState.Pressed)
            {
                mode.propModels = null;
                change?.Invoke(mode);
            }
        }
        
        protected override void OnDrop(DragEventArgs e)
        {
            base.OnDrop(e);
            if (e.Data.GetDataPresent(typeof(PropModel)))
            {
              var data=   e.Data.GetData(typeof(PropModel)) as PropModel;
                if (data.BuWei== zhuangBeiLanType)
                {
                    mode.propModels= data;
                }
            }
        }

        private void can_MouseDown(object sender, MouseButtonEventArgs e)
        {
            click?.Invoke(mode, zhuangBeiLanType);
        }
    }
}
